package vy.org.app.page.printlabel.group;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.vy.dal.dao.ViewVyPrintLabelDAO;
import org.vy.dal.dao.ViewVyPrintLabelDO;

import vy.org.app.common.AppConstants;
import vy.org.app.common.BaseAction;
import vy.org.app.common.ModuleEnum;
import vy.org.app.page.subscription.magazine.MagazineHelper;

public class AddGroupPrintLabelAction extends BaseAction {

	@Override
	public ActionForward executeAction(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		try{
			String ids[] = request.getParameterValues("subId");
			List<ViewVyPrintLabelDO> results = (List<ViewVyPrintLabelDO>)ViewVyPrintLabelDAO.findBySubscriptionIds(ids);
			List<GroupPrintLabelForm> lst = new ArrayList<GroupPrintLabelForm>();
			for(ViewVyPrintLabelDO dto : results){
				GroupPrintLabelForm form = MagazineHelper.fillMagazineGroupForm(dto, null);
				lst.add(form);
			}
			if(actionForm == null)
				request.setAttribute("groupPrintLabelForm", actionForm);
			else{
				GroupPrintLabelForm form = new GroupPrintLabelForm();
				form.setOps(request.getParameter("ops"));
				request.setAttribute("groupPrintLabelForm", form);
			}
			request.setAttribute(AppConstants.RESULT, lst);
			
			return mapping.findForward("success");
		}catch(Exception e){
			e.printStackTrace();
		}
		return mapping.findForward("failure");
	}

	@Override
	protected ModuleEnum getModuleId() {
		return ModuleEnum.EDIT_SUBSCRIPTION;
	}

}
